Accusoft.OCRXpress.Java
Tutorial: Create Your First Project

In this step-by-step tutorial we’ll build a complete application to recognize text in an image and output all the individual words and their locations to the terminal.

Before running this tutorial a license for OCRXpress for Java should already be installed.

If you have run the setup script in the install folder then you should have an evaluation license already. If not, then see License Manager to install a license.

For this tutorial, we are using the Eclipse IDE.

  1. Create a new Java project in Eclipse and name it OCR_Tutorial.
  2. Once the project is created add the ocrxpressjava-3.2.jar to the project libraries.
    1. Right-click the project.
    2. Select Properties.
    3. Open Java build path.
    4. Choose Libraries.
    5. Click Add external jars.
    6. Navigate to the install location "/path/to/install/location/Accusoft/OCRXpressJava3-64/bin”.
    7. Select ocrxpressjava-3.2.jar and click OK.
  3. Create a new main class.
    1. Right-click the project.
    2. Click New > Class.
    3. Assign a name to the project (that describes its contents), like “OCRMain”.
    4. (Optional) Specify an appropriate package name.
    5. Select Create a public static void main stub.
    6. Finally, click Finish.
  4. Import what you need with the following code:
    Copy Code
    import com.accusoft.ocrxpress.Document;
    import com.accusoft.ocrxpress.OcrxException;
    import com.accusoft.ocrxpress.OcrXpress;
    import com.accusoft.ocrxpress.RecognitionParameters;
    import com.accusoft.ocrxpress.Language;
    import com.accusoft.ocrxpress.Word;
    import com.accusoft.ocrxpress.Rectangle;
    
    import java.awt.image.*;
    import javax.imageio.*;
    import java.io.*;
    
  5. Add some code to display a message that the sample is starting and then load an image:
    Copy Code
    System.out.println("\n....................... Begin Tutorial .......................\n");
    String inputImagePath = "/path/to/install/location/Accusoft/OCRXpressJava3-64/samples/images/text.bmp";
    //Read in the image from inputImagePath
    BufferedImage bufferedImg = null;
    try {
       bufferedImg = ImageIO.read(new File(inputImagePath));
    }
    catch (IOException e) {
       System.out.println("Failed to open the image file: ");
       e.printStackTrace();
       return;
    }
    
  6. You will need to modify the "inputImagePath" to your OCRXpress install location.
    • Simply replace the text "/path/to/install/location/" with the location to which you unpacked the .tar file.
  7. Set the tessdata directory to the dependencies or where your project is running from.
    Copy Code
    OcrXpress.setDataPrefixPath("/path/to/install/location/Accusoft/OCRXpressJava3-64/bin/");
    
  8. Again, replace the text "/path/to/install/location/" with the location to which you unpacked the OCRXpress .tar file.
  9. Create your objects that will perform recognition:
    Copy Code
    RecognitionParameters params = new RecognitionParameters();
    OcrXpress ocrEngine = new OcrXpress();
    Document recognitionResults;
    
  10. Set the language and perform the recognition of the image:
    Copy Code
    try {
       params.setLanguage(Language.ENGLISH);
       System.out.println("Performing ocr on the image file.");
       recognitionResults = ocrEngine.recognizeToMemory(params, bufferedImg);
    
  11. Iterate through the words and display them to the console:
    Copy Code
    for (Word wd : recognitionResults.getWords()) {
      System.out.println(wd.toString() + " at (" + wd.getArea().getLeft() + "," + wd.getArea().getTop() + ")");
    }
    } //end of try
    
  12. Add a catch block and finish the tutorial:
    Copy Code
    catch (OcrxException ex){
       System.out.println("Recognition failure: " + ex.getMessage());
    }
    System.out.println("Finished tutorial.");
    
  13. You can now run the application and see the recognized words printed to the console.

 

 

 


©2016. Accusoft Corporation. All Rights Reserved.

Send Feedback